# LOAD PACKAGES
library("ggplot2")
library("scales")
library("dplyr")
library("reshape2")
getwd()
##########################

# Re-order function
reorder_size <- function(x) {
  factor(x, levels = names(sort(table(x))))
}
##########################

# Data GSC Q33a internal contacts
df1 <- data.frame(read.csv(file="./gsc_data_for_plots.csv", header = T, row.names=NULL, sep=",", na.strings=""))
dim(df1)
head(df1)
df1$type <- NA
df1$type[df1$V1>0.5] <- 'Siloed'
df1$type[df1$V2>0.5] <- 'Boundary-spanners'
df1$type[df1$V3>0.5] <- 'Unit-focused'
df1$resp


# Data ECFTF internal contacts
df2 <- data.frame(read.csv(file="./ecftf_data_for_plots.csv", header = T, row.names=NULL, sep=",", na.strings=""))
dim(df2)
head(df2)

df2$type <- NA
df2$type[df2$V1>0.5] <- 'Collaborative'
df2$type[df2$V2>0.5] <- 'Unit-focused'
df2$type[df2$V3>0.5] <- 'Managers'
df2$type[df2$V4>0.5] <- 'Boundary-spanners'
df2$type[df2$V5>0.5] <- 'Isolates'


########################## GSC PLOTS #####################################

# Distribution of GSC respondents Q33a by area of responsibility
ggplot(df1, aes(reorder_size(resp))) + geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels = percent) +
  ggtitle("GSC Dataset - Distribution of tasks") +
  labs(x = "Tasks", y="Percentage")
last_plot() + coord_flip()

# Plot with percentages inside the bar charts!
df <- df1 %>% dplyr::count(resp1, type) %>%    # Group by responsibility and type, then count number in each group
  mutate(pct=n/sum(n)) # calculate percentages

str(df)
df$type <- factor((df$type),
                   levels = c("Boundary-spanners", "Unit-focused", "Siloed"))

plot1 <- ggplot(df,             
       aes(reorder_size(resp1), n, fill=type)) +
  geom_bar(stat="identity") +
  geom_text(aes(label=paste0(sprintf("%1.f", pct*100),"%")), 
         position=position_stack(vjust=0.6), size=4, check_overlap = T) +
  ggtitle("GSC Blockmodel of Internal Contacts") +
  labs(x = "Tasks", y="Number of respondents")
plot1 + scale_fill_manual(values = c("Boundary-spanners" = "#535a54", "Unit-focused" = "#bec1bf", "Siloed" = "#e9eae9")) + 
  theme_light()  
last_plot() + coord_flip()
  
# Plot with facets = type of employee, y axis = Task, color = DG!!!
ggplot(df1, aes(reorder_size(resp1), fill=DG_type)) + geom_bar(aes(y = (..count..)/sum(..count..)*100)) + 
  scale_y_continuous(labels=function(x) paste0(x,"%")) + 
  facet_wrap( ~ type) +
  ggtitle("GSC Blockmodel of Internal Contacts") +
  labs(x = "DG", y="Percentage of employees of each type") +
  guides(color=guide_legend(title="Task", direction="vertical", ncol=1)) +
  theme(legend.key = element_rect(size = 0.2), legend.key.size = unit(0.5, "cm"),legend.key.width = unit(0.5, "cm"),
        legend.title=element_text(size = 9),
        legend.text=element_text(size=)) 
last_plot() + coord_flip()



########################## EC PLOTS #######################################

# Distribution of EC respondents Q34a by area of responsibility
ggplot(df2, aes(reorder_size(Resp))) + geom_bar(aes(y = (..count..)/sum(..count..))) + 
  scale_y_continuous(labels = percent) +
  ggtitle("ECFTF Dataset - Distribution of tasks") +
  labs(x = "Tasks", y="Percentage")
last_plot() + coord_flip()

# Plot with percentages inside the bar charts!
dff <- df2 %>% count(Resp, type) %>%    # Group by region and species, then count number in each group
  mutate(pct=n/sum(n))
head(dff)

head(dff)
dff$type <- factor((dff$type),
  levels = c("Boundary-spanners", "Collaborative",  "Unit-focused", "Managers", "Isolates"))

plot2 <- ggplot(dff,             
       aes(reorder_size(Resp), n, fill=type)) +
  geom_bar(stat="identity") +
  geom_text(aes(label=paste0(sprintf("%1.f", pct*100),"%")), 
            position=position_stack(vjust=0.6), size=4, check_overlap = T) +
  ggtitle("EC Blockmodel of Internal Contacts") +
  labs(x = "Tasks", y="Number of respondents")
plot2 + scale_fill_manual(values = c("Boundary-spanners" = "#535a54", "Collaborative" = "#7e847f", "Unit-focused" = "#bec1bf", "Managers" = "#d4d6d4", "Isolates" = "#e9eae9")) +
  theme_light()
last_plot() + coord_flip()


# Plot with facets = type of employee, y axis = Task, color = DG!!!

# exclude detached and isolates
df2$type2 <- NA
df2$type2[df2$V1>0.5] <- 'Collaborative'
df2$type2[df2$V2>0.5] <- 'Unit-focused'
df2$type2[df2$V4>0.5] <- 'Boundary-spanners'

df2 <- df2[complete.cases(df2),]
head(df2)

# Only show selected DGs, otherwise you cannot see anything

selected <- c("DGT", "MARKT", "COMP", "SANCO", "TRADE", "Cabinet", "SCIC", "JRC")
dff2 <- df2[df2$DG_type %in% selected,]
head(dff2)

ggplot(dff2, aes(reorder_size(Resp), fill=DG_type)) + geom_bar(aes(y = (..count..)/sum(..count..)*100)) + 
  scale_y_continuous(labels=function(x) paste0(x,"%")) + 
  facet_wrap( ~ type2) +
  ggtitle("EC Blockmodel of Internal Contacts") +
  labs(x = "Tasks", y="Percentage of employees of each type") +
  guides(color=guide_legend(title="DG", direction="vertical", ncol=1)) +
  theme(legend.key = element_rect(size = 0.2), legend.key.size = unit(0.5, "cm"),legend.key.width = unit(0.5, "cm"),
        legend.title=element_text(size = 8),
        legend.text=element_text(size=7)) 
last_plot() + coord_flip()
